{
  "displayName": "Madoko",
  "name": "madoko",
  "mimeTypes": ["text/madoko"],
  "fileExtensions": ["mdk","md"],
  "editorOptions": { "tabSize": 4, "insertSpaces": true },
  "ignoreCase": true,

  "autoClosingPairs": [ ["\"","\""],["{","}"],["[","]"],["(",")"] ],
  "noindentBrackets": "~+",

  
  "lineComment": "",
  "blockCommentStart": "<!--\n",
  "blockCommentEnd":   "\n-->",

  "wordDefinition": "[\\w\\-:]+",

  "escapes":  "\\\\(?:[^a-zA-Z0-9\\r]|$)",  

  
  "jsescapes":  "\\\\(?:[btnfr\\\\\"']|[0-7][0-7]?|[0-3][0-7]{2})",
  
  "metakey": "^(?:@(\\w+) +)?((?:\\w|([\\.#~])(?=[\\w\\-]))(?:[\\w\\-\\.#~]|, *)*(?: +?[\\w\\-]+){0,2}\\*?)[ \\t]*(?:[:]|(\\{))",  

  "metadata": [
    "Title","Subtitle","Sub Title","Title Note", "Author","Author Note","Affiliation","Email",
    "Toc Depth","Heading Depth",
    "Heading Base","Section Depth","Section Base","Bib","Bibliography",
    "Bib Style","Biblio Style","Bibliography Style","Cite Style","Cite All","Bib Search Url",
    "Bibtex","Pdf Latex","Fragment End","Fragment Start","Colorizer",
    "Copyright","License","Keywords","Comment","Revision","Address","Phone",
    "Css","Script","Html Header","Html Footer","Html Meta","Css Header",
    "Math Mode","Math Embed","Math Dir","Math Dpi","Math Scale",
    "Latex","Convert","Dvipng","Math Pdf Latex","MathJax","MathJax Ext",
    "Doc Class","Document Class","Package","Tex Header","Tex Header*","Package*",
    "Beamer Theme","Beamer Theme Options",
    "Reveal Theme","Reveal Url","Reveal Details","Reveal Long","Reveal SlideNumber",
    "Description","Documentation",
    "Embed", "Embed Limit", "Math Embed Limit",
    "Logo","Refer","Classification","Title Running","Author Running","Author Columns", "Maketitle",
    "Html Bib Label Width",
    "Csl Style", "Locale",
    "Tex2", "Tex Section Num", "Bib Label",
    "lowline","qed","vfill","eg","ie","etal","smallskip","medskip","bigskip",
    "LogoLatex","LogoTex","LogoBibtex", "LogoAmsTex", "LogoMadoko"
  ],
  
  
  "empty": [
    "area", "base", "basefont", "br", "col", "frame", 
    "hr", "img", "input", "isindex", "link", "meta", "param"
  ],
  
  "latexblock": "equation|texraw|math|mathpre|mathdefs|snippet",

  "tokenizer": {
    "root": [
      ["^(\\t|[ ]{4}(?:\\S|\\s+\\S)*)(\\s*$)", ["namespace.code","invalid"], "@codeblock" ], 
      {"include": "@rootnocode" }
    ],
    
    "rootnocode": [
      
      ["(?!^)[\\w\\s\\(\\)\\-,\\.?;]+",""],
      
      
      ["^(\\s{0,3})(\\[)(TITLE|FOOTNOTES|BIB|TOC)(\\])(\\s*)", 
        ["white","@brackets.special","namespace.metadata.special.$3","@brackets.special","white"]],
      ["^(\\s{0,3})(\\[)(TOC|INCLUDE)(=|:)([^\\]\\r\\n]+)(\\])(\\s*)", 
        ["white","@brackets.special","namespace.metadata.special.$3", 
          "namespace.metadata.special", 
          "attrs.string.escape", 
          "@brackets.special","white"]],
      
      ["^@(?:if|supports)\\b.*\\{", { "token": "namespace.metadata.key", "next": "metadatax" } ],

      ["^@metakey", { "cases": {
        "$4": { "token": "namespace.metadata.key", "next": "metadatax" },
        "$2@metadata": { "token": "namespace.metadata.key", "next": "metadata" },
        "$3": { "token": "namespace.metadata.key", "next": "metadata" },
        "@default": { "token": "" } 
      }}],
      
      
      ["^(\\s{0,3})(#+)0?((?:[^\\\\\\{]|@escapes)+)", ["white","keyword.heading.level$2.delimiter","keyword.heading.level$2"]],
      ["^\\s{0,3}(=+|\\-+)\\s*$", "keyword.heading.level0.delimiter"],      
      ["^\\s{0,3}(([\\*_\\-]\\s*){3})\\s*(?=$|\\{)", "keyword.header.hr"],
      ["^(\\s{0,3})(~+)(\\s*(?:begin\\s+)?(?:@latexblock)\\s*)(\\{)([^\\}]+)(\\})(\\s*)$",
        ["white","keyword.header.custom.delimiter",
          { "token": "keyword.header.custom.$2.delimiter", "bracket": "@open" },
          "@brackets.attrs.string.escape","attrs.string.escape",
          { "token": "@brackets.attrs.string.escape", "nextEmbedded":"text/latex", "next":"@xlatexdisplay.$2" },
          "white"
        ]
      ],
      ["^\\s{0,3}(~+)\\s*(?:begin\\s+([\\w\\-]+)\\s*|end\\s+([\\w\\-]+)\\s*|([\\w\\-]+)\\s*)?(?=(?:\\{[^}]+\\}?\\s*)?$)", {
        "cases": {
          "$2": { "token": "keyword.header.custom.$2.delimiter", "bracket": "@open" },
          "$3": { "token": "keyword.header.custom.$3.delimiter", "bracket": "@close" },
          "$4~(@latexblock)": { "token": "keyword.header.custom.$1.delimiter", "bracket": "@open", "nextEmbedded": "text/latex", "next": "@xlatexdisplay.$1" },
          "$4": { "token": "keyword.header.custom.$1.delimiter", "bracket": "@open" },
          "@default": { "token": "keyword.header.custom.$1.delimiter", "bracket": "@close" }
        }}],      
      
      ["^\\s{0,3}>+", "keyword.quote" ],  
      
      
      
      ["^\\s*`(``+)\\s*(?:([^\\s\\{]+)\\s*)?(?:\\{[^}]+\\}?\\s*)?$", { "cases": {
        "$2==javascript": { "token": "keyword.header.codeblock.delimiter", "bracket": "@open", "next": "@codeblockgh.$1.javascript", "nextEmbedded": "text/javascript" },
        "$2==json": { "token": "keyword.header.codeblock.delimiter", "bracket": "@open", "next": "@codeblockgh.$1.json", "nextEmbedded": "application/json" },
        "$2~\\w+/.*": { "token": "keyword.header.codeblock.delimiter", "bracket": "@open", "next": "@codeblockgh.$1.$2", "nextEmbedded": "$2" },
        "$2": { "token": "keyword.header.codeblock.delimiter", "bracket": "@open", "next": "@codeblockgh.$1.$2", "nextEmbedded": "text/$2" },
        "@default": { "token": "keyword.header.codeblock.delimiter", "bracket": "@open", "next": "@codeblockgh.$1" }
      }}],
      
      
      
      ["^(\\s*)([\\*\\-+:]|\\d\\.)([ \\t]\\s*)", { "token": "keyword.list", "next": "@list.$1 $3" } ],  
      
      
      ["^(\\s{0,3})(\\[)(\\^[^\\]]+)(\\])([ \\t]*:)",
        ["white","@brackets.link","string.escape.link.footnote","@brackets.link",
          { "token": "string.escape.definition.footnote", "next": "@indented" }
        ]
      ],
      ["^(\\s{0,3})(\\[)([^\\]]+)(\\])([ \\t]*:[^\\{]*)",  
        ["white","@brackets.link","string.escape.link","@brackets.link","string.escape.definition"]
      ],
      
      
      { "include": "@linecontent" } 
    ], 
    
    "list": [
      ["^\\S", { "token": "@rematch", "next": "@pop" }],
      { "include": "@rootnocode" }
    ],
    
    "codeline": [
      ["(.*)", { "token": "namespace.code", "next": "@pop" } ]
    ],
    
    "metadata": [
      ["^(@metakey)", { "token": "@rematch", "next": "@pop" } ],
      ["^(?!\\s\\s\\s)", { "token": "@rematch", "next": "@pop" }],
      [".+", "attrs.string.escape" ]
    ],

    "metadatax": [
      ["[^\\{\\}\\\\]+", { "token": "attrs.string.escape" } ],
      ["\\\\.", { "token": "attrs.string.escape" } ],
      ["\\{", { "token": "attrs.string.escape", "next": "@push" } ],
      ["\\}", { "token": "attrs.string.escape", "next": "@pop" }],
      [".", { "token":"@rematch", "next": "@pop" }]
    ],
    
    "codeblock": [      
      ["^((?:\\t|[ ]{4})(?:\\S|\\s+\\S)*)(\\s*)$", ["namespace.code","invalid"] ], 
      [".", { "token": "@rematch", "next": "@pop"} ]
    ],

    
    "codeblockgh": [      
      ["\\s*`(?=(``+)\\s*$)", { "cases": {
        "$1==$S2": { "cases": {
          "$S3": { "token": "@rematch", "switchTo":"codeblockgh_end", "nextEmbedded": "@pop" },
          "@default": { "token": "@rematch", "switchTo":"codeblockgh_end"  }
        }},
        "@default": "namespace.code" 
      }} ],
      ["\\s+$", "invalid"],
      ["(?:\\S|\\s+\\S)+", "namespace.code" ],
      ["[^`]*$", "namespace.code" ] 
    ],
    
    "codeblockgh_end": [
      [".`*", { "token": "keyword.header.codeblock.delimiter", "next": "@pop", "bracket": "@close" } ]  
    ],
        
    "xlatexdisplay_start": [
      ["(\\s*)(\\{)([^}]+)(\\})", ["white","@brackets.attrs.string.escape","attrs.string.escape","@brackets.attrs.string.escape"]],
      [".|$", { "token": "", "nextEmbedded": "text/latex", "switchTo": "@xlatexdisplay.$S2" }] 
    ],
    
    "xlatexdisplay": [
      ["^\\s*(~+)\\s*$", { "cases": {
        "$1==$S2": { "token": "@rematch", "switchTo": "@xlatexdisplay_end", "nextEmbedded": "@pop" }
      }}]      
    ],
    
    "xlatexdisplay_end": [
      ["^\\s*(~+)\\s*$", { "token": "keyword.header.custom.$1.delimiter", "next": "@popall", "bracket": "@close" } ],
      [".|$", { "token": "@rematch", "next": "@pop" } ]
    ],
    
    "xlatexinline": [
      ["([^\\\\\\$%]|\\\\.)+(?![\\r\\n]|$)", "latex.source" ],
      ["%[^\\$]*\\r?$", "latex.source" ],
      [".|\\r|\\n", { "token": "@rematch", "nextEmbedded": "@pop", "switchTo":"@xlatexinline_end"} ]      
    ],
    
    "xlatexinline_end": [
      ["\\$",   { "token": "math.delimiter", "bracket": "@close", "next": "@pop" }],
      [".|$",  { "token": "math.delimiter.invalid", "next": "@pop" }]
    ],
    
    "indented": [
      ["^\\s{0,2}\\S", { "token": "@rematch", "next": "@pop"} ],
      {"include": "@linecontent" } 
    ],
    
    "linecontent": [ 
      
      ["&#?\\w+;", "entity.string"],      
      ["@escapes", "entity.string" ],
      
      
      ["\\b(__)((?:[^\\\\_]|@escapes|_(?!_))+)(__)\\b", ["strong.delimiter","strong","strong.delimiter"]],
      ["(\\*\\*)((?:[^\\\\*]|@escapes|\\*(?!\\*))+)(\\*\\*)", ["strong.delimiter","strong","strong.delimiter"]],
      ["\\b(_)([^_]+)(_)\\b", ["emphasis.delimiter","emphasis","emphasis.delimiter"]],
      ["(\\*)((?:[^\\\\*]|@escapes)+)(\\*)", ["emphasis.delimiter","emphasis","emphasis.delimiter"]],
      ["(`)((?:[^`])+)(`)", ["namespace.code.delimiter","namespace.code","namespace.code.delimiter"] ],
      ["\\$(?!\\$|$)", { "token": "math.delimiter", "next": "@xlatexinline", "nextEmbedded": "text/latex", "bracket": "@open" } ],
      ["<<|>>", ""],
      
      
      ["(\\{)([^}]+)(\\})", ["@brackets.attrs.string.escape","attrs.string.escape","@brackets.attrs.string.escape"]],
      ["(!?\\[)((?:#|\\^|[\\-\\+!]?\\@)(?:[^\\]\\\\]|@escapes)+)(\\])(?![ \\t]*[\\[\\(\\{])",
        [{"token":"delimiter.square.link.internal", "bracket":"@open"},"string.link.internal",{"token":"delimiter.square.link.internal", "bracket":"@close"}]],          
      ["(!?\\[)((?:[^\\]\\\\]|@escapes)+)(\\])(?![ \\t]*[\\[\\(\\{])",
        [{"token":"delimiter.square.link", "bracket":"@open"},"string.link",{"token":"delimiter.square.link", "bracket":"@close"}]],          
      ["(!?\\[)", {"token":"delimiter.square.link", "bracket":"@open"} ],
      ["(\\])([ \\t]*)(\\[)((?:[^\\]\\\\]|@escapes)+)(\\])", [{"token":"delimiter.square.link", "bracket":"@close"},"white","@brackets.link","string.escape.link","@brackets.link"]],
      ["(\\])([ \\t]*)(\\()((?:[^\\)\\\\]|@escapes)+)(\\))", [{"token":"delimiter.square.link", "bracket":"@close"},"white","@brackets.link","string.escape.link","@brackets.link"]],
      ["\\](?=[ \\t]*\\{)", {"token":"delimiter.square.link", "bracket":"@close"}],
      
      { "include": "html" }, 
      
      ["\\w+|\\-+|[\\.,;]+",""]
    ],
    
    "html": [
      
      ["<(\\w+)\\/>", "tag.tag-$1" ],
      ["^ *<(\\w+)(?=\\s*[\\/>]|\\s+\\w)",  {"cases": { "@empty":   { "token": "tag.tag-$1", "next": "@tag.$1" },
                            "@default": { "token": "tag.tag-$1", "bracket": "@open", "next": "@tag.$1" } }}],
      ["^ *<\\/(\\w+)\\s*>",  { "token": "tag.tag-$1", "bracket": "@close", "next": "@pop" } ],
      
      
      { "include": "@whitespace" }      
    ],
    
    
    
    "whitespace": [
      ["[ \\t]{2}$", "invalid"],
      ["^[ \\t]+$", "invalid"],
      ["[ \\t\\r\\n]+", ""],
      ["<!--", "comment", "@comment"]
    ],
    
    "comment": [
      ["[^<\\-]+", "comment.content" ],
      ["-->", "comment", "@pop" ],
      ["<!--", "comment.content.invalid"],
      ["[<\\-]", "comment.content" ]
    ],
        
    
    "tag": [
      ["[ \\t\\r\\n]+", "white" ],
      ["(type)(\\s*=\\s*)(\")([^\"]+)(\")", [ "attribute.name", "delimiter", "attribute.value",
                                         {"token": "attribute.value", "switchTo": "@tag.$S2.$4" },
                                         "attribute.value"] ], 
      ["(type)(\\s*=\\s*)(')([^']+)(')", [ "attribute.name", "delimiter", "attribute.value",
                                         {"token": "attribute.value", "switchTo": "@tag.$S2.$4" },
                                         "attribute.value"] ], 
      ["(\\w+)(\\s*=\\s*)(\"[^\"]*\"|'[^']*')", ["attribute.name","delimiter","attribute.value"]],
      ["\\w+", "attribute.name" ],      
      ["\\/>", "tag.tag-$S2", "@pop"],
      [">", { "cases": { "$S2==style" : { "token": "tag.tag-$S2", "switchTo": "@embedded.$S2", "nextEmbedded": "text/css"},
                       "$S2==script": { "cases": { "$S3"     : { "token": "tag.tag-$S2", "switchTo": "@embedded.$S2", "nextEmbedded": "$S3" },
                                                 "@default": { "token": "tag.tag-$S2", "switchTo": "@embedded.$S2", "nextEmbedded": "text/javascript" } } },
                       "@default"   : { "token": "tag.tag-$S2", "switchTo": "html" } } }]
    ],
    
    "embedded": [
      ["[^\"'<]+", ""],
      ["<\\/(\\w+)\\s*>", { "cases": { "$1==$S2" : { "token": "@rematch", "switchTo": "@html", "nextEmbedded": "@pop" },
                                  "@default": "" } }],
      ["\"([^\"\\\\]|\\\\.)*$", "string.invalid" ],  
      ["'([^'\\\\]|\\\\.)*$", "string.invalid" ],  
      ["\"", "string", "@string.\"" ],
      ["'", "string", "@string.'" ],
      ["<", ""]
    ],
    
    
    "string": [
      ["[^\\\\\"']+", "string"], 
      ["@jsescapes", "string.escape"],
      ["\\\\.",      "string.escape.invalid"],
      ["[\"']",     { "cases": { "$#==$S2" : { "token": "string", "next": "@pop" },
                              "@default": "string" }} ]
    ]

  }
}